<template>
  <div >
    <div class="page-section">  
      <div class="page-title">知识库</div>
         <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-input
          v-model="dataForm.key"
          placeholder="请输入查询关键字"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.pcodeDesc" placeholder="请选择类型">
          <el-option
            v-for="category in categoryList"
            :label="category.issueDesc"
            :value="category.issueDesc"
            :key="category.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">搜索</el-button>
        <el-button type="primary" @click="addOrUpdateHandle(0)">添加</el-button>
        <!-- <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    </div>
 
    <el-table
      :data="dataList"
      border
      :header-cell-style="{ backgroundColor: '#f2f2f2' }"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <el-table-column
        prop="pcodeDesc"
        header-align="center"
        align="center"
        label="类别名称"
      >
      </el-table-column>
      <el-table-column
        prop="issueDesc"
        header-align="center"
        align="center"
        label="问题"
      >
      </el-table-column>
      <el-table-column
        prop="solution"
        header-align="center"
        align="center"
        label="答案"
      >
      </el-table-column>
      <el-table-column
        prop="waiterId"
        header-align="center"
        align="center"
        label="创建客服"
      >
      </el-table-column>
      <el-table-column
        prop="creactTime"
        header-align="center"
        align="center"
        label="创建时间"
      >
        <template slot-scope="{ row }">
          {{ row.creactTime | knowledgeTimeFormat }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="addOrUpdateHandle(1, scope.row)"
            >修改</el-link
          >
          <el-link type="danger" @click="deleteHandle(scope.row)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-size="pageSize"
      :total="totalPage"
      layout="total,  prev, pager, next"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->

      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="refreshDataList"
      ></add-or-update>

  </div>
</template>

<script>
import AddOrUpdate from "./components/knowledgelabel-add-or-update";
import { queryList, queryTreeList, remove } from "@/api/knowlegeBase";
import { mapState } from "vuex";
export default {
  data() {
    return {
      dataForm: {
        key: "",
        pcodeDesc: "全部",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      categoryList: [],
    };
  },
  components: {
    AddOrUpdate,
  },
  computed: {
    ...mapState(["idStore"]),
  },
  mounted() {
    this.getDataList();
    this.getCategory();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      const params = {
        pageNum: this.pageIndex,
        pageSize: this.pageSize,
        searchName: this.dataForm.key,
        pcodeDesc:
          this.dataForm.pcodeDesc === "全部" ? "" : this.dataForm.pcodeDesc,
      };
      queryList(params)
        .then((res) => {
          const {
            data: { records, total },
          } = res;
          this.dataList = records;
          this.totalPage = total;
        })
        .catch(() => {
          this.$message.error(`获取知识库异常`);
        })
        .finally(() => {
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(type, data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(type, data);
      });
    },
    // 删除
    deleteHandle(data) {
      this.$confirm(`确定删除?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.delete(data);
      });
    },
    delete(data) {
      // const { id, code,pcode } = data;
      // const params = { id, code,pcode };
      if(this.dataList.length == 1){ // 
        this.pageIndex = 1
      }
      remove(data)
        .then(() => {
          this.$message.success("删除成功！");
          this.getDataList();
        })
        .catch((err) => {
          this.$message(`删除知识库数据接口异常${err}`);
        });
    },
    getCategory() {
      const params = {
        pageSize: 1000,
        pageNum: 1,
      };
      queryTreeList(params)
        .then((res) => {
          const { data } = res;
          this.categoryList = data.filter((item) => {
            if (item.listTree) return item;
          });
          this.categoryList.unshift({ id: 0, issueDesc: "全部" });
        })
        .catch(() => {
          this.$message.error(`获取知识库异常`);
        });
    },
    refreshDataList() {
      this.getDataList();
      this.getCategory();
    },
  },
};
</script>
<style lang="scss" scoped>
.knowlegelabel-contaner {
  padding: 0 10px;
}
.el-link {
  margin: 4px;
}
</style>